from flask_admin.contrib.sqla import ModelView
from flask_admin.contrib import rediscli
from flask_admin.contrib.fileadmin import FileAdmin

from app.settings.constants import BASE_URL_PREFIX


from flask_admin import Admin
from app import redis_clent
from app.models.hello import Hello
from app.models.user import User
from app.models.order import Order
from app.models import db


class OrderAdmin(ModelView):
    column_labels = {
        "id": "id",
        "order_id": "订单ID",
        "title": "电影名",
        "ticket_code": "取票码",
        "check_code": "验证码",
        "pirce": "价格",
        "site": "场次",
        "cinema": "电影院",
        "create_date": "创建时间",
        "user_id": "关联用户",
    }


class UserAdmin(ModelView):
    column_labels = {
        "id": "用户ID",
        "username": "用户名",
        "password": "密码",
        "email": "邮件",
        "activation": "是否激活",
        "superuser": "超级用户",
        "vipuser": "VIP用户",
        "orders": "用户订单",
    }


class HelloAdmin(ModelView):
    column_labels = {
        "id": "用户ID",
        "first_name": "用户姓",
        "last_name": "用户名",
    }

    column_list = ["id", "first_name", "last_name"]


def create_model_admin(app):
    # set optional bootswatch theme
    admin = Admin(
        app, name="flask-admin", template_mode="bootstrap3", url=BASE_URL_PREFIX + "/admin"
    )

    # Add administrative views here
    admin.add_view(HelloAdmin(Hello, db.session))
    admin.add_view(UserAdmin(User, db.session))
    admin.add_view(OrderAdmin(Order, db.session))

    # Add redis admin
    admin.add_view(rediscli.RedisCli(redis_clent))
